/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package it.trick.admin;

import it.trick.admin.controller.ChannelTopController;
import it.trick.admin.controller.SystemErrorPopWindowController;
import java.util.logging.Level;
import java.util.logging.Logger;
import javafx.application.Application;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.scene.image.Image;
import javafx.scene.layout.Pane;
import javafx.stage.Modality;
import javafx.stage.Stage;
import javafx.stage.StageBuilder;

/**
 *
 * @author takahara
 */
public class App extends Application {

    /**
     * App instance
     */
    private static App instance;
    /**
     * メインステージ
     */
    private Stage stage;
    /**
     * ポップアップステージ
     */
    private Stage popStage;

    private String site = "http://it-trick-javafx.appspot.com/";

    /**
     * コンストラクタ
     */
    public App() {

        this.setInstance();
    }

    /**
     * 初期化メソッド
     */
    private void setInstance() {
        instance = this;
    }

    /**
     * ステージスタート
     *
     * @param primaryStage
     * @throws Exception
     */
    @Override
    public void start(Stage primaryStage) throws Exception {

        try {
            stage = primaryStage;
            stage.setMinWidth(Constants.MINIMUM_WINDOW_WIDTH);
            stage.setMinHeight(Constants.MINIMUM_WINDOW_HEIGHT);
            stage.setWidth(Constants.MINIMUM_WINDOW_WIDTH);
            stage.getIcons().add(new Image(App.class.getResourceAsStream("resource/images/app_icon.png")));

            // ログイン画面をデフォルト表示
            this.gotoChannelManage();
            stage.show();

            initPopStage();

        } catch (Exception ex) {
            Logger.getLogger(App.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    private void initPopStage() {
        popStage = new Stage();

        popStage.initOwner(this.getStage());
        popStage.initModality(Modality.APPLICATION_MODAL);
    }

    /**
     * Get App Instance
     *
     * @return
     */
    public static App getInstance() {
        return instance;
    }

    /**
     * ステージオブジェクトの取得
     *
     * @return
     */
    public Stage getStage() {
        return stage;
    }

    public String getSite() {
        return this.site;
    }

    public void setSite(String url) {
        this.site = url;
    }

    /**
     * チャンネル管理へ
     */
    private void gotoChannelManage() {
        stage.setTitle("IT TRICK");
        ChannelTopController controller = new ChannelTopController();
        this.replaceSceneContent(controller);
    }

    /**
     * ページ遷移
     *
     * @param controller
     */
    private void replaceSceneContent(Parent controller) {
        Scene scene = stage.getScene();
        if (scene == null) {
            scene = new Scene(controller);
            stage.setScene(scene);
        } else {
            stage.getScene().setRoot(controller);
        }
    }

    /**
     * エラーポップアップ画面を表示
     */
    public void showErrorPop(Exception exception) {

        Parent controller = new SystemErrorPopWindowController(exception);

        Scene scene = new Scene(controller);
        Stage errorPopStage = StageBuilder.create()
                .scene(scene)
                .title("システムエラー")
                .width(600)
                .height(400)
                .build();

        errorPopStage.initOwner(stage);
        errorPopStage.initModality(Modality.APPLICATION_MODAL);
        errorPopStage.show();
    }

    /**
     * ポップアップ画面を表示
     */
    public void showPopStage(Pane controller, String title, double width, double height, boolean waitFlg) {
        popStage.setTitle(title);
        popStage.setWidth(width);
        popStage.setHeight(height);
        Scene scene = popStage.getScene();
        if (scene == null) {
            scene = new Scene(controller);
            popStage.setScene(scene);
        } else {
            popStage.getScene().setRoot(controller);
        }

        if(waitFlg) {
            popStage.showAndWait();
        } else {
            popStage.show();
        }
    }

    /**
     * ポップアップ画面を閉じる
     */
    public void closePopStage() {
        popStage.close();
    }

    public void replacePopSceneContent(Pane controller) {
        Scene scene = popStage.getScene();
        if (scene == null) {
            scene = new Scene(controller);
            popStage.setScene(scene);
        } else {
            popStage.getScene().setRoot(controller);
        }
    }

    /**
     * The main() method is ignored in correctly deployed JavaFX application.
     * main() serves only as fallback in case the application can not be
     * launched through deployment artifacts, e.g., in IDEs with limited FX
     * support. NetBeans ignores main().
     *
     * @param args the command line arguments
     */
    public static void main(String[] args) {
        launch(args);
    }
}
